local skel = fk.CreateSkill {
  name = "emo__ezhao",
}

Fk:loadTranslationTable{
  ["emo__ezhao"] = "噩兆",
  [":emo__ezhao"] = "每回合限一次，你每对一名其他角色使用X张实体牌，便可以摸两张牌并视为对其使用一张【杀】（X为其体力值）。",
  ["@emo__ezhao"] = "噩兆",

  ["#emo__ezhao-ask"] = "噩兆：你可以摸2张牌，并视为对 %src 使用【杀】",
}

skel:addEffect(fk.TargetSpecified, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      if player:usedSkillTimes(skel.name) > 0 then return false end
      return data.extra_data and data.extra_data[skel.name] and table.contains(data.extra_data[skel.name], data.to)
    end
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#emo__ezhao-ask:"..data.to.id }) then
      event:setCostData(self, { tos = { data.to } })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    table.removeOne(data.extra_data[skel.name], data.to)
    player:drawCards(2, skel.name)
    local to = data.to
    if not player.dead and not to.dead then
      room:useVirtualCard("slash", nil, player, to, skel.name, true)
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and
    not (data.card:isVirtual() and #data.card.subcards == 0)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local tos = {} ---@type ServerPlayer[]
    for _, to in ipairs(data.tos) do
      if not to.dead and to ~= player then
        table.insertIfNeed(tos, to) -- 防止重复目标的情况
      end
    end
    local record = player:getTableMark("_emo__ezhao")
    for _, to in ipairs(tos) do
      room:addPlayerMark(to, "@emo__ezhao") -- 仅用于显示
      record[to.id] = (record[to.id] or 0) + 1
      if record[to.id] >= to.hp then
        record[to.id] = nil
        room:setPlayerMark(to, "@emo__ezhao", 0)
        data.extra_data = data.extra_data or {}
        data.extra_data[skel.name] = data.extra_data[skel.name] or {}
        table.insert(data.extra_data[skel.name], to)
      end
    end
    room:setPlayerMark(player, "_emo__ezhao", record)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  for _, p in ipairs(player.room.alive_players) do
    player.room:setPlayerMark(p, "@emo__ezhao", 0) -- 这标记没实际效果，直接清了
  end
end)

return skel
